<?php
/**
 * 文章分类管理程序
 * ============================================================================
 * 版权所有 2005-2010 KnifeCMS，并保留所有权利。
 * 网站地址: http://www.knifecms.com；
 * ----------------------------------------------------------------------------
*/
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
$exc = new exchange($ecs->table("article_cat"), $db, 'cat_id', 'cat_name');
/* act操作项的初始化 */
$_REQUEST['act'] = trim($_REQUEST['act']);
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'index';
}

if ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('article_cat');
    $cat_type = 1;
    if ($_REQUEST['parent'] > 0)
    {
        $sql = "SELECT cat_type FROM " . $ecs->table('article_cat') . " WHERE cat_id = ".$_REQUEST['parent'];
        $p_cat_type = $db->getOne($sql);
        if ($p_cat_type == 2 || $p_cat_type == 3 || $p_cat_type == 5)
        {
            sys_msg($_LANG['not_allow_add'], 0);
        }
        else if ($p_cat_type == 4)
        {
            $cat_type = 5;
        }
    }
    $sql = "INSERT INTO ".$ecs->table('article_cat')."(cat_name, cat_type, parent_id)
           VALUES ('".$_REQUEST['name']."','$cat_type',".$_REQUEST['parent'].")";
    $db->query($sql);
    $ret_cat_id=$db->insert_id();
	admin_log($_REQUEST['name'],'add','articlecat');
    $link[0]['text'] = $_LANG['continue_add'];
    $link[0]['href'] = 'articlecat.php?act=add';
    $link[1]['text'] = $_LANG['back_list'];
    $link[1]['href'] = 'articlecat.php?act=list';
    //clear_cache_files();
	//echo $db->insert_id();
	echo $ret_cat_id;
    //sys_msg($_POST['cat_name'].$_LANG['catadd_succed'],0, $link);
}
elseif ($_REQUEST['act'] == 'rename')
{
    /* 权限判断 */
    admin_priv('article_cat');
		$dat = $db->getOne("SELECT cat_name, show_in_nav FROM ". $ecs->table('article_cat') . " WHERE cat_id = '" . $_REQUEST['id'] . "'");
		if ($exc->edit("cat_name = '".$_REQUEST['name']."'",  $_REQUEST['id']))
    {
        admin_log($_REQUEST['name'], 'edit', 'articlecat');
        //clear_cache_files();
        //sys_msg($note, 0, $link);
    }
    else
    {
        die($db->error());
    }
}
/*------------------------------------------------------ */
//-- 编辑文章分类的排序
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_sort_order')
{
    check_authz_json('article_cat');
    $id    = intval($_POST['id']);
    $order = json_str_iconv(trim($_POST['val']));
    /* 检查输入的值是否合法 */
    if (!preg_match("/^[0-9]+$/", $order))
    {
        make_json_error(sprintf($_LANG['enter_int'], $order));
    }
    else
    {
        if ($exc->edit("sort_order = '$order'", $id))
        {
            clear_cache_files();
            make_json_result(stripslashes($order));
        }
        else
        {
            make_json_error($db->error());
        }
    }
}
/*------------------------------------------------------ */
//-- 删除文章分类
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('article_cat');
    $id = intval($_GET['id']);
    $sql = "SELECT cat_type FROM " . $ecs->table('article_cat') . " WHERE cat_id = '$id'";
    $cat_type = $db->getOne($sql);
    if ($cat_type == 2 || $cat_type == 3 || $cat_type ==4)
    {
        /* 系统保留分类，不能删除 */
        make_json_error($_LANG['not_allow_remove']);
    }
    $sql = "SELECT COUNT(*) FROM " . $ecs->table('article_cat') . " WHERE parent_id = '$id'";
    if ($db->getOne($sql) > 0)
    {
        /* 还有子分类，不能删除 */
        make_json_error($_LANG['is_fullcat']);
    }
    /* 非空的分类不允许删除 */
    $sql = "SELECT COUNT(*) FROM ".$ecs->table('article')." WHERE cat_id = '$id'";
    if ($db->getOne($sql) > 0)
    {
        make_json_error(sprintf($_LANG['not_emptycat']));
    }
    else
    {
        $exc->drop($id);
        $db->query("DELETE FROM " . $ecs->table('nav') . "WHERE  ctype = 'a' AND cid = '$id' AND type = 'middle'");
        //clear_cache_files();
        admin_log($cat_name, 'remove', 'category');
    }
    $url = 'articlecat.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    //ecs_header("Location: $url\n");
    exit;
}
/*------------------------------------------------------ */
//-- 编辑文章分类的排序
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'apply_sub_cat')
{
    check_authz_json('article_cat');
    $cat_arr = array();
    $cat_id = intval($_GET['id']);
    $user = $_GET['user'];
    $cat_arr = article_cat_list($cat_id, 0, false);
    foreach($cat_arr as $row)
    {
    	$sql="update " . $ecs->table('article_cat') . " set cat_user='" . $user . "' where cat_id=" . $row['id'];
    	$db->query($sql);
    }
    echo "ok";
}
/**
 * 添加商品分类
 *
 * @param   integer $cat_id
 * @param   array   $args
 *
 * @return  mix
 */
function cat_update($cat_id, $args)
{
    if (empty($args) || empty($cat_id))
    {
        return false;
    }
    return $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('article_cat'), $args, 'update', "cat_id='$cat_id'");
}
?>
